//
//  YKOrigninalView.m
//  youkan
//
//  Created by mac mini on 16/3/3.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKOrigninalView.h"
#import "YKStatusFrame.h"
#import "YKWBStatues.h"
#import "UIImageView+WebCache.h"
#import "YKPhotosView.h"
#import "YKUser.h"
@interface YKOrigninalView()
@property(nonatomic,weak)    UIImageView *iconView ;
@property(nonatomic,weak)    UILabel *nameView;
@property(nonatomic,weak)    UILabel *textView;
@property(nonatomic,weak)    UIImageView *VIPView  ;
@property(nonatomic,weak)    UILabel *timeView ;
@property(nonatomic,weak)    UILabel *sourceView ;
@property(nonatomic,weak)    YKPhotosView *photosView;
@end
@implementation YKOrigninalView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChildView];
        self.userInteractionEnabled =YES;
        self.image =[UIImage imageWithStretchableName:@"timeline_card_top_background"];
    }
    return self;
}
//添加所有子控件
-(void)setUpAllChildView{
    //!>    头像
    UIImageView *iconView =[[UIImageView alloc]init];
//    iconView.backgroundColor =[UIColor yellowColor];
    [self addSubview:iconView];
    _iconView =iconView;
//    _iconView.backgroundColor =[UIColor grayColor];
    //!>    昵称
    UILabel *nameView=[[UILabel alloc]init];
    nameView.font =YKNameFont;
    [self addSubview:nameView];
    _nameView =nameView;

    //!>    正文
    UILabel *textView=[[UILabel alloc]init];
    textView.font =YKTextFont;
    textView.numberOfLines = 0;
    [self addSubview:textView];
    _textView =textView;
    
    //!>    VIP
    UIImageView *VIPView =[[UIImageView alloc]init];
    [self addSubview:VIPView];
    _VIPView = VIPView;
    
    //!>    时间
    UILabel *timeView=[[UILabel alloc]init];
    timeView.font =YKTimeFont;
    timeView.textColor =[UIColor orangeColor];
    [self addSubview:timeView];
    _timeView =timeView;
    
    //!>    来源
    UILabel *sourceView=[[UILabel alloc]init];
    sourceView.font =YKSouresFont;
    [self addSubview:sourceView];
    _sourceView =sourceView;
    
    //!>    配图
    YKPhotosView *photosView=[[YKPhotosView alloc]init];
    [self addSubview:photosView];
    _photosView =photosView;
}
-(void)setStatusF:(YKStatusFrame *)statusF{
    _statusF =statusF;
    [self setUpFrame]; //!> 设置Frame
    [self setUpData];  //!> 设置数据

}
//设置数据来源
-(void)setUpData{
    YKWBStatues *status = _statusF.status;
//    头像
    [_iconView sd_setImageWithURL:status.user.profile_image_url placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
//    时间
    _timeView.text = status.created_at;
//    VIP
    NSString *imageName =[NSString stringWithFormat:@"common_icon_membership_level%d",status.user.mbrank];
    _VIPView.image =[UIImage imageNamed:imageName];
    if (status.user.vip) {
        _nameView.textColor =[UIColor redColor];
    }else{
        _nameView.textColor =[UIColor blackColor];
    }
//    昵称
    _nameView.text =status.user.name;
//    来源
    _sourceView.text =status.source;
//    正文
    _textView.text =status.text;
//    配图数据来源
    _photosView.pic_urls =status.pic_urls;
    
}
//!>    设置frame
-(void)setUpFrame{
//    头像
    _iconView.frame =_statusF.oriIconF;
//昵称
    _nameView.frame =_statusF.oriNameF;
//vip
    if (_statusF.status.user.vip) {
        _VIPView.hidden =NO;
        _VIPView.frame =_statusF.oriVIPF;
    }else{
        _VIPView.hidden =YES;
    }
//    时间 每次有跟新都需要计算
    YKWBStatues *status=_statusF.status;
    CGFloat timeX = _nameView.frame.origin.x;
    CGFloat timeY = CGRectGetMaxY(_nameView.frame)+YKStatusCellMargin*0.5;
//    虽然方法过期，但是还是可以使用的而且使用相对简单
//    CGSize timeSize =[status.created_at sizeWithFont:YKTimeFont];
    CGSize timeSize =[status.created_at sizeWithAttributes:@{NSFontAttributeName:YKTimeFont}];
    _timeView.frame =(CGRect){{timeX,timeY},timeSize};
//    _timeView.frame =_statusF.oriTimeF;
//    来源
    CGFloat sourceX =CGRectGetMaxX(_timeView.frame)+YKStatusCellMargin;
    CGFloat sourceY = timeY;
//    CGSize sourceSize =[status.source sizeWithFont:YKSouresFont];
    CGSize sourceSize =[status.source sizeWithAttributes:@{NSFontAttributeName:YKSouresFont}];
//    通过结构体转为CGRect
    _sourceView.frame = (CGRect){{sourceX,sourceY},sourceSize};
    _sourceView.textColor =[UIColor lightGrayColor];
//    _sourceView.frame =_statusF.oriSourceF;
//    正文
    _textView.frame =_statusF.oriTextF;
//    配图
    _photosView.frame =_statusF.oriPhtotsF;
}
@end
